package com.gjzhao.fmmall.controller;

import com.gjzhao.fmmall.entity.Users;
import com.gjzhao.fmmall.service.UsersService;
import com.gjzhao.fmmall.vo.RespResultVO;
import com.gjzhao.fmmall.vo.ResponseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Description: 用户管理
 * @Author: gjzhao
 * @Date Created in 2021-10-30 2:28 下午
 */
@RestController
@RequestMapping("/user")
@CrossOrigin //处理前后端跨域问题：由于前端和后端使用的protocol、ip、port的不一致造成跨域访问，数据不能正常响应
@Api(value = "对用户数据进行crud", tags = "用户管理")
//@Scope("singleton") //防止注入多个Service，可指定单例，可将该注解提到Controller层，默认为单例
public class UserController extends BaseController {

    @Autowired
    private UsersService usersService;

    @PostMapping(value = "/register")
    @ApiOperation(value = "注册用户")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "用户名", name = "username", dataType = "string", required = true),
        @ApiImplicitParam(value = "密码", name = "password", dataType = "string", required = true)
    })
    public ResponseResultVO addUser(String username, String password) {
        return usersService.insertUser(username, password);
    }


    @GetMapping("/login")
    @ApiOperation(value = "登录用户(普通方式)")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "用户名", name = "username", dataType = "string", required = true),
        @ApiImplicitParam(value = "密码", name = "password", dataType = "string", required = true)
    })
    public ResponseResultVO getUser(@RequestParam("username") String username, @RequestParam("password") String password) {
        ResponseResultVO resultVO = usersService.getUserByUsername(username, password);
        return resultVO;
    }

    @GetMapping("/loginByToken")
    @ApiOperation(value = "登录用户(使用token方式)")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "用户名", name = "username", dataType = "string", required = true),
        @ApiImplicitParam(value = "密码", name = "password", dataType = "string", required = true)
    })
    public ResponseResultVO getUserByToken(@RequestParam("username") String username,
                                           @RequestParam("password") String password) {
        ResponseResultVO resultVO = usersService.getUserByUsernameByToken(username, password);
        return resultVO;
    }

    @GetMapping("/checkToken")
    @ApiOperation(value = "token校验")
    @ApiImplicitParam(value = "授权令牌", name = "token", dataType = "string", required = true)
    public ResponseResultVO checkToken(@RequestParam("token") String token) {

        ResponseResultVO resultVO = usersService.checkToken(token);
        logger.info("校验token成功");
        return resultVO;
    }

    /**
     * 接口作用：校验当前 token 是否过期 业务流程分析： 1.用户点击用户中心，首先从cookie中获取token信息， - 1.1 如果为空，则跳转到登录页面(同时携带回调的url) - 1.2 如果不为空，则调用该接口验证token是否过期 - 1.2.1 如果token没有过期，则展示用户名与用户头像 - 1.2.2 如果token过期了，跳转到登录页面(同时携带回调的url)
     *
     * @param token
     * @return
     */
    @GetMapping("/checkTokenTimeOut")
    @ApiOperation(value = "检查token是否过期")
    public ResponseResultVO checkTokenTimeOut(@RequestParam("token") String token) {
        ResponseResultVO resultVO = usersService.checkTokenByInterceptor(token);
        logger.info("校验token成功");
        return resultVO;
    }


    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除用户")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "用户名字", name = "username", dataType = "string", required = true),
        @ApiImplicitParam(value = "用户密码", name = "password", dataType = "string", required = true)
    })
    public RespResultVO deleteUser(@RequestHeader("token") String token,
                                   @RequestParam("username") String username,
                                   @RequestParam("password") String password) {

        return usersService.deleteUser(username, password);
    }


    //todo 修改用户头像(用户上传完图片之后，如何保存图片获取uri存到数据库,同时如何进行展示)
    @PutMapping(value = "/update")
    @ApiOperation(value = "更新用户信息")
    public ResponseResultVO getUser(Users user) {
        return new ResponseResultVO(200, "更新成功", null);
    }


    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    @ApiOperation(value = "用于演示@ApiIgnore")
    @ApiIgnore
    public RespResultVO demoUser() {
        //return new ResponseResultVO(200, "成功", null);
        return RespResultVO.success();
    }


    /**
     * 接口作用：查询当前登录用户详细信息(账户/昵称/真实名字/性别/注册时间...)
     * 业务流程：当用户进入个人信息页时，调用该接口
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("/getUserInfo")
    @ApiOperation(value = "查询用户信息")
    @ApiImplicitParam(value = "用户id", name = "userId", dataType = "string", required = true)
    public ResponseResultVO getUserInfo(@RequestHeader("token") String token,
                                        @RequestParam("userId") String userId) {
        ResponseResultVO resultVO = usersService.selectUserByUserId(userId);
        return resultVO;
    }


}

